﻿/* Problem 6. Maximal K sum

Write a program that reads two integer numbers N and K and an array of N elements from the console.
Find in the array those K elements that have maximal sum.
 */

using System;


namespace _06.MaximalKSum
{
    class MaximalKSum
    {
        static void Main()
        {
            Console.WriteLine("Enter array dimenson");
            int N = int.Parse(Console.ReadLine());
            Console.WriteLine("Enter number of elements to sum");
            int K = int.Parse(Console.ReadLine());
            int[] arrayWithNumbers = new int[N];
            int currentSum = new int();
            int maxSum = new int();
            int startIndex = new int();
            for (int i = 0; i < arrayWithNumbers.Length; i++)
            {
                Console.WriteLine("Enter number in the from the array");
                arrayWithNumbers[i] = int.Parse(Console.ReadLine());
            }
            for (int i = 0; i <= arrayWithNumbers.Length-K; i++)
            {
                currentSum = 0;
                
                for (int j = i; j < K+i; j++)
                {
                    currentSum += arrayWithNumbers[j];
                }
                if (currentSum > maxSum)
                {
                    maxSum = currentSum;
                    startIndex = i;
                }
            }
            for (int i = startIndex; i < startIndex+K; i++)
            {
                Console.Write("{0}, ",arrayWithNumbers[i]);
            }
            Console.WriteLine("maximum sum from K elements is: "+maxSum);
        }
    }
}
